package com.tyk.bookstore.front.message.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tyk.bookstore.front.common.core.util.AssertUtil;
import com.tyk.bookstore.front.message.model.entity.InfoNoticeDo;
import com.tyk.bookstore.front.message.model.query.InfoNoticeListQuery;
import com.tyk.bookstore.front.message.model.query.InfoNoticePageQuery;
import com.tyk.bookstore.front.message.model.result.InfoNoticeResultEnum;
import com.tyk.bookstore.front.message.model.vo.InfoNoticeListVo;
import com.tyk.bookstore.front.message.model.vo.InfoNoticePageVo;
import com.tyk.bookstore.front.message.model.vo.InfoNoticeVo;
import com.tyk.bookstore.front.message.repository.InfoNoticeRepository;
import com.tyk.bookstore.front.message.service.InfoNoticeService;
import com.tyk.bookstore.front.message.util.MapStructMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class InfoNoticeServiceImp implements InfoNoticeService {
    private final InfoNoticeRepository infoNoticeRepository;
    private final MapStructMessage mapStructMessage;


    @Override
    public InfoNoticeVo getById(Long id) {
        InfoNoticeDo notice = infoNoticeRepository.selectById(id);
        AssertUtil.notNull(notice, InfoNoticeResultEnum.NOTICE_NOT_FOUND);
        return mapStructMessage.toInfoNoticeVo(notice);
    }

    @Override
    public List<InfoNoticeListVo> getList(InfoNoticeListQuery query) {
        return infoNoticeRepository.selectInfoNoticeSimList(query.getSize());
    }

    @Override
    public InfoNoticePageVo getPage(InfoNoticePageQuery query) {
        Page<InfoNoticeDo> page = new Page<>(query.getPage(), query.getLimit());
        LambdaQueryWrapper<InfoNoticeDo> w = Wrappers.lambdaQuery();
        infoNoticeRepository.selectPage(page, w);
        return mapStructMessage.toInfoNoticePageVo(page);
    }
}
